<template>
  <div class="goodsinfo">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 轮播图区域 -->
    <div class="panel">
      <swiper :lunbolist="lunbolist" class="my-swiper"></swiper>
    </div>

    <!-- 列表信息 -->
    <van-panel class="panel" :title="goodsinfo.title">
      <div class="panel-body">
        <div class="price-group">
          市场价格:
          <span class="old">{{goodsinfo.market_price}}</span>
          销售价格:
          <span class="new">{{goodsinfo.sell_price}}</span>
        </div>
        <div class="stepper">
          <div class="number">购买数量:</div>
          <!--
            :min="1" 默认就是1 不添加也可以
            plus 点击添加的时候触发
            minus 点击减少的时候触发
            step 默认走一步
          -->
          <van-stepper v-model="value" integer class="number" :step="step"/>
        </div>
        <div class="btn-group">
          <van-button type="primary" size="small">立即购买</van-button>
          <van-button type="danger" size="small" @click="addToShopCart" :disabled="btnFlag">加入购物车</van-button>
        </div>
      </div>
    </van-panel>

    <!-- 列表信息 -->
    <van-panel class="panel" title="商品信息">
      <div class="panel-body">
        <p>商品货号：{{ goodsinfo.goods_no }}</p>
        <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
        <p>上架时间：{{ goodsinfo.add_time | datefmt }}</p>
      </div>
    </van-panel>

    <div>
      <p>1</p>
      <p>2</p>
      <p>3</p>
      <p>4</p>
      <p>5</p>
      <p>6</p>
      <p>7</p>
      <p>8</p>
      <p>9</p>
      <p>10</p>
      <p>11</p>
      <p>12</p>
      <p>13</p>
      <p>14</p>
      <p>15</p>
      <p>16</p>
      <p>17</p>
      <p>18</p>
      <p>19</p>
      <p>20</p>
      <p>21</p>
      <p>22</p>
      <p>23</p>
      <p>24</p>
      <p>25</p>
      <p>26</p>
      <p>27</p>
      <p>28</p>
      <p>29</p>
      <p>30</p>
      <p>31</p>
      <p>32</p>
      <p>33</p>
      <p>34</p>
      <p>35</p>
      <p>36</p>
      <p>37</p>
      <p>38</p>
      <p>39</p>
      <p>40</p>
      <p>41</p>
      <p>42</p>
      <p>43</p>
      <p>44</p>
      <p>45</p>
      <p>46</p>
      <p>47</p>
      <p>48</p>
      <p>49</p>
      <p>50</p>
      <p>51</p>
      <p>52</p>
      <p>53</p>
      <p>54</p>
      <p>55</p>
      <p>56</p>
      <p>57</p>
      <p>58</p>
      <p>59</p>
      <p>60</p>
      <p>61</p>
      <p>62</p>
      <p>63</p>
      <p>64</p>
      <p>65</p>
      <p>66</p>
      <p>67</p>
      <p>68</p>
      <p>69</p>
      <p>70</p>
      <p>71</p>
      <p>72</p>
      <p>73</p>
      <p>74</p>
      <p>75</p>
      <p>76</p>
      <p>77</p>
      <p>78</p>
      <p>79</p>
      <p>80</p>
      <p>81</p>
      <p>82</p>
      <p>83</p>
      <p>84</p>
      <p>85</p>
      <p>86</p>
      <p>87</p>
      <p>88</p>
      <p>89</p>
      <p>90</p>
      <p>91</p>
      <p>92</p>
      <p>93</p>
      <p>94</p>
      <p>95</p>
      <p>96</p>
      <p>97</p>
      <p>98</p>
      <p>99</p>
      <p>100</p>
    </div>
  </div>
</template>
<script>
import swiper from "../../components/swiper";
export default {
  data() {
    return {
      step: 1,
      value: 0,
      id: this.$route.params.id,
      lunbolist: [],
      goodsinfo: {},
      ballFlag: false,
      xDist: 0,
      yDist: 0,
      btnFlag: false
    };
  },
  created() {
    this.getthumimages();
    this.getgoodsinfo();
  },
  methods: {
    /**
     * 获取缩略图数组
     */
    async getthumimages() {
      // const id = Number(this.id) + 100;
      const {
        data: { status, message }
      } = await this.$http.get("api/getthumimages/" + this.id);
      if (status === 0) {
        this.lunbolist = message;
      } else {
        Toast("获取缩略图数据失败");
      }
    },
    /**
     * 获取商品的信息
     */
    async getgoodsinfo() {
      const {
        data: { status, message }
      } = await this.$http.get("api/goods/getinfo/" + this.id);
      if (status === 0) {
        this.goodsinfo = message;
        // console.log(message);
      } else {
        Toast("获取商品信息失败");
      }
    },
    getBount() {
      // 获取起始的位置
      let ball = this.$refs.ball.getBoundingClientRect();

      // 获取目的地的位置
      let info = document.querySelector(".van-info").getBoundingClientRect();

      let xDist = info.left - ball.left;
      let yDist = info.top - ball.top;

      this.xDist = xDist;
      this.yDist = yDist;
    },
    addToShopCart() {
      this.ballFlag = !this.ballFlag;
      this.btnFlag = !this.btnFlag; // true
      setTimeout(() => {
        this.btnFlag = !this.btnFlag; // false
      }, 500);

      // 先包装要添加的数据对象

      // console.log(this.$route.params.id);
      var goodsinfo = {
        id: this.$route.params.id,
        count: this.value,
        sell_price: this.goodsinfo.sell_price,
        selected: true,
        title: this.goodsinfo.title,
        src: this.lunbolist[0].src,
        timer: null
      };
      // 调用mutations里面的函数更新 state
      this.$store.dispatch("addToCart", goodsinfo);
    },
    beforeEnter(el) {
      // 设置元素动画的起始位置
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      // 强制刷新
      el.offsetTop;

      // 获取移动距离
      this.getBount();

      // 动画的目的地
      el.style.transform = `translate(${this.xDist}px,${this.yDist}px)`;
      // 过渡时间
      el.style.transition = "all .5s ease";
      done();
    },
    afterEnter() {
      this.ballFlag = !this.ballFlag;
    }
  },
  components: {
    swiper
  }
};
</script>
<style lang="less">
.goodsinfo {
  position: relative;
  .ball {
    width: 15px;
    height: 15px;
    background: red;
    border-radius: 50%;
    position: absolute;
    left: 140px;
    top: 260px;
    z-index: 520;
  }
  .panel {
    width: 96%;
    border-radius: 6px;
    box-shadow: 0 0 3px #ccc;
    margin: 0 auto;
    margin-top: 10px;
    overflow: hidden;
    .my-swiper {
      text-align: center;
      height: 166px;
      line-height: 166px;
      img {
        width: 100%;
        vertical-align: middle;
      }
    }

    .panel-body {
      margin-left: 16px;
      margin-bottom: 10px;
      color: #8f8f94;
      font-size: 14px;
      .price-group {
        margin-bottom: 12px;
        .old {
          text-decoration: line-through;
          margin-right: 6px;
        }
      }
      .stepper {
        color: #8f8f94;
        font-size: 0;
        .number {
          display: inline-block;
          font-size: 14px;
          margin-right: 6px;
        }
      }
      .btn-group {
        margin-top: 10px;
      }
      p {
        margin-top: 10px;
      }
    }
  }
}
</style>